<?php
declare (strict_types=1);

namespace app\common\model;

class BusinessCensus extends Base
{

    protected $name = 'business_census';

    public static function getCensus($id, $filed, $date = '')
    {
        return parent::where(['create_date' => $date ?: date('Y-m-d'), 'business_id' => $id])->field($filed)->find();
    }

    public static function getWeekCensus($id, $filed)
    {
        return parent::where('business_id', $id)->whereTime('create_time', 'week')->sum($filed);
    }

    public static function getCommonCensus($business, $param = [])
    {
        $todayCensus = parent::where(['business_id' => $business->id])->whereDay('create_time')->field('order_income,order_count')->find();
        $weekCensus = parent::where(['business_id' => $business->id])->whereWeek('create_time')->field('sum(order_income) as order_income, sum(order_count) as order_count')->find();
        $monthCensus = parent::where(['business_id' => $business->id])->whereMonth('create_time')->field('sum(order_income) as order_income, sum(order_count) as order_count')->find();

        //销售统计
        $result['incomeCensus'] = [
            'sum'   => floatval($business->sum_order_income),
            'today' => $todayCensus ? floatval($todayCensus->order_income) : '0.00',
            'week'  => $weekCensus  ? floatval($weekCensus->order_income) : '0.00',
            'month' => $monthCensus ? floatval($monthCensus->order_income) : '0.00'
        ];
        //订单统计
        $result['orderCensus'] = [
            'sum'   => (int)$business->sum_order_count,
            'today' => $todayCensus ? (int)$todayCensus->order_count : 0,
            'week'  => $weekCensus ? (int)$weekCensus->order_count : 0,
            'month' => $monthCensus ? (int)$monthCensus->order_count : 0
        ];
        //其他统计
        $where = ['business_id'=>$business->id, 'is_del'=>0];
        $result['otherCensus'] = [
            'fans'          => $business->fans,
            'today_visitor' => UserBusinessView::where($where)->whereDay('create_time')->count(),
            'collect'       => UserCollectProduct::where($where)->count()
        ];

        if(!empty($param['year'])){
            $result['diagramCensus'] = self::getYearCensus($business->id, 'order_income', $param['year']);
        }else{
            $month = !empty($param['month']) ? $param['month'] : date("Y-m");
            $result['diagramCensus'] = self::getMonthCensus($business->id, 'order_income', $month);
        }

        return $result;
    }

    public static function getYearCensus($id, $filed, $year)
    {
        $list = parent::where(['year' => $year, 'business_id' => $id])->field("year,month,sum($filed) as value")->group('month')->select();

        $list = array_column($list->toArray(),null,'month');

        for ($i = 1; $i <= 12; $i++) {
            $m = $i < 10 ? '0' . $i : $i.'';

            if(!isset($list[$m])){
                $list[$m] = [
                    'year'  => $year,
                    'month'  => $m,
                    'value' => '0.00',
                ];
            }
        }

        array_multisort($list, SORT_ASC);

        return array_values($list);
    }

    public static function getMonthCensus($id, $filed, $date)
    {
        list($year, $month) = explode('-', $date);
        $list = parent::where(['year' => $year, 'month'=>$month, 'business_id' => $id])->field("year,month,day,sum($filed) as value")->group('day')->select();

        $list = array_column($list->toArray(),null,'day');


        $days = date("t", strtotime($date));
        for ($i = 1; $i <= $days; $i++) {
            $d = $i < 10 ? '0' . $i : $i.'';

            if(!isset($list[$d])){
                $list[$d] = [
                    'year'  => $year,
                    'month' => $month,
                    'day'   => $d,
                    'value' => '0.00',
                ];
            }
        }

        array_multisort($list, SORT_ASC);

        return array_values($list);
    }

}
